home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 2: Applications / Linux Cubed Series 2 - Applications.iso / editors / emacs / xemacs / xemacs-1.006 / xemacs-1 / lib / xemacs-19.13 / lisp / modes / rsz-minibuf.el < prev    next >
Encoding:
Text File  |  1995-08-22  |  9.0 KB  |  243 lines

  1. ;;; rsz-minibuf.el --- dynamically resize minibuffer to display entire contents
  2.  
  3. ;;; Copyright (C) 1990 Roland McGrath
  4. ;;; Copyright (C) 1993, 1994 Noah S. Friedman
  5.  
  6. ;;; Author: Noah Friedman <friedman@prep.ai.mit.edu>
  7. ;;;         Roland McGrath <roland@prep.ai.mit.edu>
  8. ;;; Modified for Lucid Emacs By: Peter Stout <pds@cs.cmu.edu>
  9. ;;; Maintainer: friedman@prep.ai.mit.edu
  10. ;;; Keywords: minibuffer, window, frames, display
  11. ;;; Status: Known to work in FSF GNU Emacs 19.23 and Lucid Emacs 19.9.
  12.  
  13. ;;; $Id: rsz-minibuf.el,v 1.9 1994/04/08 09:18:59 friedman Exp $
  14.  
  15. ;;; This program is free software; you can redistribute it and/or modify
  16. ;;; it under the terms of the GNU General Public License as published by
  17. ;;; the Free Software Foundation; either version 2, or (at your option)
  18. ;;; any later version.
  19. ;;;
  20. ;;; This program is distributed in the hope that it will be useful,
  21. ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  23. ;;; GNU General Public License for more details.
  24. ;;;
  25. ;;; You should have received a copy of the GNU General Public License
  26. ;;; along with this program; if not, you can either send email to this
  27. ;;; program's maintainer or write to: The Free Software Foundation,
  28. ;;; Inc.; 675 Massachusetts Avenue; Cambridge, MA 02139, USA.
  29.  
  30. ;;; Commentary:
  31.  
  32. ;;; This package allows the entire contents (or as much as possible) of the
  33. ;;; minibuffer to be visible at once when typing.  As the end of a line is
  34. ;;; reached, the minibuffer will resize itself.  When the user is done
  35. ;;; typing, the minibuffer will return to its original size.
  36.  
  37. ;;; In window systems where it is possible to have a frame in which the
  38. ;;; minibuffer is the only window, the frame itself can be resized.  In FSF
  39. ;;; GNU Emacs 19.22 and earlier, the frame may not be properly returned to
  40. ;;; its original size after it ceases to be active because
  41. ;;; `minibuffer-exit-hook' didn't exist until version 19.23.
  42.  
  43. ;;; NOTE: The code to resize frames has not been tested under Lucid Emacs,
  44. ;;; because detached minibuffers are broken.
  45.  
  46. ;;; Note that the minibuffer and echo area are not the same!  They simply
  47. ;;; happen to occupy roughly the same place on the frame.  Messages put in
  48. ;;; the echo area will not cause any resizing by this package.
  49.  
  50. ;;; This package is considered a minor mode but it doesn't put anything in
  51. ;;; minor-mode-alist because this mode is specific to the minibuffer, which
  52. ;;; has no modeline.
  53.  
  54. ;;; To use this package, put the following in your .emacs:
  55. ;;;
  56. ;;;     (autoload 'resize-minibuffer-mode "rsz-minibuf" nil t)
  57. ;;;
  58. ;;; Invoking the command `resize-minibuffer-mode' will then enable this mode.
  59.  
  60. ;;; Code:
  61.  
  62.  
  63. ;;;###autoload
  64. (defvar resize-minibuffer-mode nil
  65.   "*If non-`nil', resize the minibuffer so its entire contents are visible.")
  66.  
  67. ;;;###autoload
  68. (defvar resize-minibuffer-window-max-height nil
  69.   "*Maximum size the minibuffer window is allowed to become.
  70. If less than 1 or not a number, the limit is the height of the frame in
  71. which the active minibuffer window resides.")
  72.  
  73. ;;;###autoload
  74. (defvar resize-minibuffer-window-exactly t
  75.   "*If non-`nil', make minibuffer exactly the size needed to display all its contents.
  76. Otherwise, the minibuffer window can temporarily increase in size but
  77. never get smaller while it is active.")
  78.  
  79.  
  80. ;;;###autoload
  81. (defvar resize-minibuffer-frame nil
  82.   "*If non-`nil' and the active minibuffer is the sole window in its frame, allow changing the frame height.")
  83.  
  84. ;;;###autoload
  85. (defvar resize-minibuffer-frame-max-height nil
  86.   "*Maximum size the minibuffer frame is allowed to become.
  87. If less than 1 or not a number, there is no limit.")
  88.  
  89. ;;;###autoload
  90. (defvar resize-minibuffer-frame-exactly nil
  91.   "*If non-`nil', make minibuffer frame exactly the size needed to display all its contents.
  92. Otherwise, the minibuffer frame can temporarily increase in size but
  93. never get smaller while it is active.")
  94.  
  95.  
  96. ;;;###autoload
  97. (defun resize-minibuffer-mode (&optional prefix)
  98.   "Enable or disable resize-minibuffer mode.
  99. A negative prefix argument disables this mode.  A positive argument or
  100. argument of 0 enables it.
  101.  
  102. When this minor mode is enabled, the minibuffer is dynamically resized to
  103. contain the entire region of text put in it as you type.
  104.  
  105. The variable `resize-minibuffer-mode' is set to t or nil depending on
  106. whether this mode is active or not.
  107.  
  108. The maximum height to which the minibuffer can grow is controlled by the
  109. variable `resize-minibuffer-window-max-height'.
  110.  
  111. The variable `resize-minibuffer-window-exactly' determines whether the
  112. minibuffer window should ever be shrunk to make it no larger than needed to
  113. display its contents.
  114.  
  115. When using a window system, it is possible for a minibuffer to tbe the sole
  116. window in a frame.  Since that window is already its maximum size, the only
  117. way to make more text visible at once is to increase the size of the frame.
  118. The variable `resize-minibuffer-frame' controls whether this should be
  119. done.  The variables `resize-minibuffer-frame-max-height' and
  120. `resize-minibuffer-frame-exactly' are analogous to their window
  121. counterparts."
  122.   (interactive "p")
  123.   (or prefix (setq prefix 0))
  124.   (cond
  125.    ((>= prefix 0)
  126.     (setq resize-minibuffer-mode t))
  127.    (t
  128.     (setq resize-minibuffer-mode nil))))
  129.  
  130. ;;; Glue code to make things work in both FSF and Lucid Emacsen.
  131. (if (string-match "Lucid" emacs-version)
  132.     (progn
  133.       (fset 'resize-frame-parameters 'screen-parameters)
  134.       (fset 'resize-frame-height 'screen-height)
  135.       (fset 'resize-frame-width 'screen-width)
  136.       (fset 'resize-selected-frame 'selected-screen)
  137.       (fset 'resize-set-frame-size 'set-screen-size)
  138.       (defun resize-minibuffer-frame-alist ()
  139.     "Return the frame alist for the minibuffer."
  140.     minibuffer-alist))
  141.   (fset 'resize-frame-parameters 'frame-parameters)
  142.   (fset 'resize-frame-height 'frame-height)
  143.   (fset 'resize-frame-width 'frame-width)
  144.   (fset 'resize-selected-frame 'selected-frame)
  145.   (fset 'resize-set-frame-size 'set-frame-size)
  146.   (defun resize-minibuffer-frame-alist ()
  147.     "Return the frame alist for the minibuffer."
  148.     minibuffer-frame-alist))
  149.  
  150. (defun resize-minibuffer-setup ()
  151.   (cond
  152.    (resize-minibuffer-mode
  153.     (cond
  154.      ((and window-system
  155.        (eq 'only (cdr (assq 'minibuffer (resize-frame-parameters)))))
  156.       (and resize-minibuffer-frame
  157.        (progn
  158.          (make-local-variable 'minibuffer-exit-hook)
  159.          (add-hook 'minibuffer-exit-hook 'resize-minibuffer-frame-restore)
  160.          (add-hook 'local-post-command-hook 'resize-minibuffer-frame))))
  161.      (t
  162.       (add-hook 'local-post-command-hook 'resize-minibuffer-window))))))
  163.  
  164. (defun resize-minibuffer-count-window-lines (&optional start end)
  165.   "Return number of window lines occupied by text in region.
  166. The number of window lines may be greater than the number of actual lines
  167. in the buffer if any wrap on the display due to their length.
  168.  
  169. Optional arguments START and END default to point-min and point-max,
  170. respectively."
  171.   (or start (setq start (point-min)))
  172.   (or end   (setq end   (point-max)))
  173.   (if (= start end)
  174.       0
  175.     (save-excursion
  176.       (save-restriction
  177.         (widen)
  178.     (narrow-to-region start end)
  179.     (goto-char start)
  180.         (vertical-motion (buffer-size))))))
  181.  
  182. ;; Why isn't `min' a subr?
  183. ;; Do not pretend this is a real definition of `min'.  It suffices for this
  184. ;; packages's purposes (and is reasonably fast for a lisp call) but a real
  185. ;; min function would be able to take more than 2 arguments.
  186. (defun resize-minibuffer-min (x y)
  187.   "Return the lesser of X or Y."
  188.   (if (< x y)
  189.       x
  190.     y))
  191.  
  192.  
  193. ;; Resize the minibuffer window to contain the minibuffer's contents.
  194. ;; The minibuffer must be the current window.
  195. (defun resize-minibuffer-window ()
  196.   (let ((height (window-height))
  197.     (lines (1+ (resize-minibuffer-count-window-lines))))
  198.     (and (numberp resize-minibuffer-window-max-height)
  199.      (> resize-minibuffer-window-max-height 0)
  200.      (setq lines (resize-minibuffer-min
  201.               lines
  202.               resize-minibuffer-window-max-height)))
  203.     (or (if resize-minibuffer-window-exactly
  204.         (= lines height)
  205.       (<= lines height))
  206.     (enlarge-window (- lines height)))))
  207.  
  208.  
  209. ;; Resize the minibuffer frame to contain the minibuffer's contents.
  210. ;; The minibuffer frame must be the current frame.
  211. (defun resize-minibuffer-frame ()
  212.   (let ((height (resize-frame-height))
  213.     (lines (1+ (resize-minibuffer-count-window-lines))))
  214.     (and (numberp resize-minibuffer-frame-max-height)
  215.      (> resize-minibuffer-frame-max-height 0)
  216.      (setq lines (resize-minibuffer-min
  217.               lines
  218.               resize-minibuffer-frame-max-height)))
  219.     (cond
  220.      ((> lines height)
  221.       (resize-set-frame-size (resize-selected-frame)
  222.                  (resize-frame-width)
  223.                  lines))
  224.      ((and resize-minibuffer-frame-exactly
  225.        (> height (cdr (assq 'height (resize-minibuffer-frame-alist))))
  226.        (< lines height))
  227.       (resize-set-frame-size (resize-selected-frame)
  228.                  (resize-frame-width)
  229.                  lines)))))
  230.  
  231. ;; Restore the original height of the frame.
  232. (defun resize-minibuffer-frame-restore ()
  233.   (resize-set-frame-size (resize-selected-frame)
  234.              (resize-frame-width)
  235.              (cdr (assq 'height (resize-minibuffer-frame-alist)))))
  236.  
  237.  
  238. (provide 'rsz-minibuf)
  239.  
  240. (add-hook 'minibuffer-setup-hook 'resize-minibuffer-setup)
  241.  
  242. ;; rsz-minibuf.el ends here
  243.